package com.hy.controller.setting;

import com.hy.common.base.RespResult;
import com.hy.common.excel.HyDepartmentExport;
import com.hy.common.log.annotation.SysOpLogAnnotation;
import com.hy.entity.dto.DepartmentSearchDTO;
import com.hy.entity.dto.UserSearchDTO;
import com.hy.entity.pojo.HyDepartment;
import com.hy.service.HyDepartmentService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {



    @Resource
    private HyDepartmentService hyDepartmentService;
    @Resource
    private HyDepartmentExport departmentExport;
    @GetMapping(value = "/showDepartments")
    @SysOpLogAnnotation(menuName = "部门",menuBtn = "查看部门",reqParam = "'查询条件'")
    public RespResult showUnit( DepartmentSearchDTO dto){

        return hyDepartmentService.showDepartment(dto);

    }


    @PostMapping(value = "/addDepartment")
    @SysOpLogAnnotation(menuName = "部门",menuBtn = "添加部门",reqParam = "'部门ID:'+#department.departmentId")
    public RespResult addUnit(@RequestBody HyDepartment department){



        return hyDepartmentService.addDepartment(department);
    }

    @PostMapping(value = "/deleteDepartment")
    @SysOpLogAnnotation(menuName = "部门",menuBtn = "删除部门",reqParam = "'部门ID'+#ids")
    public RespResult  addUnit(Integer[] ids){
        List<Integer> myIds=new ArrayList<>();
        Collections.addAll(myIds,ids);
        return hyDepartmentService.deleteDepartment(myIds);
    }

    @PostMapping(value = "/updateDepartment")
    @SysOpLogAnnotation(menuName = "部门",menuBtn = "修改部门",reqParam = "'修改部门'+#department.departmentId")
    public RespResult  updateUnit(@RequestBody HyDepartment department){
        if (department.getDel().equals(2)){
            return RespResult.error(500,"不存在的资源");
        }
        return hyDepartmentService.updateDepartment(department);
    }
    @PostMapping(value = "/exportExcelSmallData")
    @SysOpLogAnnotation(menuName = "部门",menuBtn = "导出部门信息",reqParam = "'导出部门'")
    public void   exportExcelSmallData(HttpServletResponse response, DepartmentSearchDTO searchDTO){
        departmentExport.exportWithBigData(response,"部门列表",searchDTO);

    }

    @RequestMapping("/importExcelBigData")
    @SysOpLogAnnotation(menuName = "部门",menuBtn = "导入部门",reqParam = "'导入部门'")
    public RespResult importExcelBigData(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request){


        return  hyDepartmentService.importExcelBigData(multipartFile,request);
    }
}
